package com.cloud.pole.common.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.concurrent.ThreadLocalRandom;

public class NumUtil {

    private NumUtil() {
    }

    ;

    public static double d2ceil(final double arg) {
        return new BigDecimal(arg).setScale(2, BigDecimal.ROUND_CEILING).doubleValue();
    }

    public static double d2half(final double arg) {
        return new BigDecimal(arg).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double d2floor(final double arg) {
        return new BigDecimal(arg).setScale(0, BigDecimal.ROUND_FLOOR).doubleValue();
    }

    public static double d0ceil(final double arg) {
        return new BigDecimal(arg).setScale(0, BigDecimal.ROUND_CEILING).doubleValue();
    }

    public static double d0half(final double arg) {
        return new BigDecimal(arg).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double d0floor(final double arg) {
        return new BigDecimal(arg).setScale(2, BigDecimal.ROUND_FLOOR).doubleValue();
    }

    public static long l0ceil(final double arg) {
        return new BigDecimal(arg).setScale(0, BigDecimal.ROUND_CEILING).longValue();
    }

    public static long l0half(final double arg) {
        return new BigDecimal(arg).setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
    }

    public static long l0floor(final double arg) {
        return new BigDecimal(arg).setScale(0, BigDecimal.ROUND_FLOOR).longValue();
    }

    public static String s2thousand(double arg) {
        return (new DecimalFormat("#,##0.00")).format(arg);
    }

    /**
     * 随机生成范围内的double
     *
     * @param arg1
     * @param arg2
     * @return
     * @throws Exception
     */
    public static double nextDouble(final double arg1, final double arg2) {
        if (arg1 == arg2) {
            return arg1;
        } else if (arg1 < arg2) {
            return getRandom().nextDouble(arg1, arg2);
        } else {
            return getRandom().nextDouble(arg2, arg1);
        }
    }

    /**
     * 随机生成范围内的long
     *
     * @param arg1
     * @param arg2
     * @return
     * @throws Exception
     */
    public static long nextLong(final long arg1, final long arg2) {
        if (arg1 == arg2) {
            return arg1;
        } else if (arg1 < arg2) {
            return getRandom().nextLong(arg1, arg2);
        } else {
            return getRandom().nextLong(arg2, arg1);
        }
    }

    public static ThreadLocalRandom getRandom() {
        return ThreadLocalRandom.current();
    }
}
